package com.sky.aspect;

import com.sky.annotation.AutoFill;
import com.sky.context.BaseContext;
import com.sky.entity.*;
import com.sky.enumeration.OperationType;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

//自定义切面类,实现字段自动填充
@Aspect
@Component
public class AutoFillAspect {

    //自动填充公共字段切入点
    @Pointcut("execution(* com.sky.mapper.*Mapper.*(..))&&@annotation(com.sky.annotation.AutoFill)")
    public void autoFill() {
    }

    @Before("autoFill()")
    public void before(JoinPoint joinPoint) {
        //先拿到拦截到的方法操作数据库的类型
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        AutoFill autoFill = signature.getMethod().getAnnotation(AutoFill.class);
        OperationType value = autoFill.value();
        Object[] args = joinPoint.getArgs();
        if (args == null || args.length == 0) {
            return;
        }
        Object arg = args[0];
        if (arg == null) {
            return;
        }
        if (value.equals(OperationType.INSERT)) {
            //插入公共字段
            if (arg instanceof Category) {
                LocalDateTime now = LocalDateTime.now();
                Category category = (Category) arg;
                category.setCreateTime(now);
                category.setUpdateTime(now);
                category.setCreateUser(BaseContext.getCurrentId());
                category.setUpdateUser(BaseContext.getCurrentId());
            } else if (arg instanceof Employee) {
                LocalDateTime now = LocalDateTime.now();
                Employee employee = (Employee) arg;
                employee.setCreateTime(now);
                employee.setUpdateTime(now);
                employee.setCreateUser(BaseContext.getCurrentId());
                employee.setUpdateUser(BaseContext.getCurrentId());
            }else if (arg instanceof Dish){
                LocalDateTime now = LocalDateTime.now();
                Dish dish = (Dish) arg;
                dish.setCreateTime(now);
                dish.setCreateTime(now);
                dish.setCreateUser(BaseContext.getCurrentId());
                dish.setUpdateUser(BaseContext.getCurrentId());
            }
            else if (arg instanceof Setmeal){
                LocalDateTime now = LocalDateTime.now();
                Setmeal setmeal = (Setmeal) arg;
                setmeal.setCreateTime(now);
                setmeal.setUpdateTime(now);
                setmeal.setCreateUser(BaseContext.getCurrentId());
                setmeal.setUpdateUser(BaseContext.getCurrentId());
            }
            else if (arg instanceof User){
                LocalDateTime now = LocalDateTime.now();
                User user = (User) arg;
                user.setCreateTime(now);
            }
        }
        else {
            //更新公共字段
            if (arg instanceof Category) {
                LocalDateTime now = LocalDateTime.now();
                Category category = (Category) arg;
                category.setUpdateTime(now);
                category.setUpdateUser(BaseContext.getCurrentId());
            } else if (arg instanceof Employee) {
                LocalDateTime now = LocalDateTime.now();
                Employee employee = (Employee) arg;
                employee.setUpdateTime(now);
                employee.setUpdateUser(BaseContext.getCurrentId());
            }
            else if (arg instanceof Dish){
                LocalDateTime now = LocalDateTime.now();
                Dish dish = (Dish) arg;
                dish.setUpdateTime(now);
                dish.setUpdateUser(BaseContext.getCurrentId());
            }
            else if (arg instanceof Setmeal){
                LocalDateTime now = LocalDateTime.now();
                Setmeal setmeal = (Setmeal) arg;
                setmeal.setUpdateTime(now);
                setmeal.setUpdateUser(BaseContext.getCurrentId());
            }
        }
    }
}
